#ifndef LED_BEE_FAN_WIDGET_H
#define LED_BEE_FAN_WIDGET_H

#include <QtMqtt/QtMqtt>
#include <QtMqtt/QMqttClient>
#include <QWidget>
#include "fsmpLed.h"
#include "fsmpBeeper.h"
#include "config.h"
#include "fsmpFan.h"
#include "fsmpVibrator.h"
#include <QPixmap>

namespace Ui {
class led_bee_fan_Widget;
}

class led_bee_fan_Widget : public QWidget
{
    Q_OBJECT

public:
    explicit led_bee_fan_Widget(QWidget *parent = 0);
    ~led_bee_fan_Widget();

    fsmpLeds *led;

    fsmpBeeper *bee;

    fsmpFan *fan;

    fsmpVibrator *vib;


    QPixmap pix;

    void onlineService()
    {
        //        client = new QMqttClient;
        //        //绑定连接成功信号
        //        connect(client,&QMqttClient::connected,this, &led_bee_fan_Widget::connectSuccess);
        //        connect(client,&QMqttClient::disconnected,this, &led_bee_fan_Widget::connectError);
        //        //绑定连接的ip
        //        client->setHostname(MQTT_HOSTNAME);
        //        //绑定端口
        //        client->setPort(MQTT_PORT);
        //        //连接服务器
        //        client->connectToHost();
        if(client != nullptr)
        {
            qDebug()<< "Is Connected";
            connect(client,&QMqttClient::messageReceived,this, &led_bee_fan_Widget::Receive);
        }
        else
        {
            qDebug()<< "Error unConnected";
        }
//        connect(client,&QMqttClient::messageReceived,this, &led_bee_fan_Widget::Receive);
    }
    void connectSuccess()
    {
        qDebug() << "connect sucess";
    }
    void connectError()
    {
        qDebug() << "connect error";
    }


    void publishLed1Msg(bool flag, int id)
    {
        QString led1msg = QString::asprintf(LAMP,flag ? "true" : "false",id);
        client->publish(QString(MQTT_DEVICE_SIM),led1msg.toLatin1().data());
        //qDebug() << led1msg;
    }

    void publishLed2Msg(bool flag, int id)
    {
        QString led2msg = QString::asprintf(LAMP,flag ? "true" : "false",id);
        client->publish(QString(MQTT_DEVICE_SIM),led2msg.toLatin1().data());
        //qDebug() << led2msg;
    }

    void publishLed3Msg(bool flag, int id)
    {
        QString led3msg = QString::asprintf(LAMP,flag ? "true" : "false",id);
        client->publish(QString(MQTT_DEVICE_SIM),led3msg.toLatin1().data());
        //qDebug() << led3msg;
    }

    void publishBeeperMsg(bool flag, int id)
    {
        QString beepermsg = QString::asprintf(ALARM,flag ? "true" : "false",id);
        client->publish(QString(MQTT_DEVICE_SIM),beepermsg.toLatin1().data());
        //qDebug() << beepermsg;
    }

    void publishFanMsg(bool flag, int id)
    {
        QString fanmsg = QString::asprintf(FAN,flag ? "true" : "false",id);
        client->publish(QString(MQTT_DEVICE_SIM),fanmsg.toLatin1().data());
        //qDebug() << fanmsg;
    }

private slots:
    void on_pushButton_led1_clicked();

    void on_pushButton_led2_clicked();

    void on_pushButton_led3_clicked();

    void on_pushButton_bee_clicked();

    void on_pushButton_fan_clicked();


    void Receive(QByteArray buf);

    void on_backButton_clicked();

private:
    Ui::led_bee_fan_Widget *ui;
};

#endif // LED_BEE_FAN_WIDGET_H
